package org.example.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0.0
 * @author: lynn
 * @description:
 * @date: 2021/8/10 11:51
 */
public class LC54 {
    public static void main(String[] args) {
        LC54 lc=new LC54();
        int[][] matrix={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        System.out.println(lc.spiralOrder(matrix));
    }

    //fixme https://www.bilibili.com/video/BV1GZ4y1H7xH?from=search&seid=12113286488134847126
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> res=new ArrayList<>();
        if (matrix==null || matrix.length==0 ||matrix[0]==null || matrix[0].length==0) return res;
        int row=matrix.length;
        int col=matrix[0].length;
        int left=0;
        int right=col-1;
        int up=0;
        int down=row-1;

        while (res.size()<row*col){

            //从左到右
            for (int i = left; i <=right ; i++) {
                if (res.size()==row*col) break;//fixme 判断下，前移动判断
                res.add(matrix[up][i]);
            }
            up++;
            //从上到下
            for (int i = up; i <=down ; i++) {
                if (res.size()==row*col) break;//fixme 判断下，前移动判断
                res.add(matrix[i][right]);
            }
            right--;
            //从右到左
            for (int i=right; i >=left ; i--) {
                if (res.size()==row*col) break;//fixme 判断下，前移动判断
                res.add(matrix[down][i]);//坐标别写错了
            }
            down--;
            for (int i = down; i >=up; i--) {
                if (res.size()==row*col) break;//fixme 判断下，前移动判断
                res.add(matrix[i][left]);
            }
            left++;
        }
        return res;
    }
}
